home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PASCTXT.ZIP / CHAP13.TXT < prev    next >
Text File  |  1988-01-15  |  15KB  |  389 lines

  1.  
  2.                   CHAPTER 13 - Units in TURBO Pascal 4.0
  3.  
  4.  
  5.                    THIS IS FOR TURBO PASCAL 4.0 USERS ONLY
  6.  
  7.              If  you are using TURBO Pascal version 3.0 or  earlier,
  8.         you will find that none of the programs in this chapter  can
  9.         be compiled or run with your system, but it would be to your
  10.         advantage to read this material anyway.
  11.  
  12.              When  Niclaus Wirth originally defined Pascal,  it  was
  13.         intended  to be a very small language to be  used  primarily
  14.         for teaching programming concepts to computer neophytes.   A
  15.         program would be contained in a single file and compiled  in
  16.         its  entirety  each  time it was  compiled.   There  was  no
  17.         provision  for  splitting a program up into  smaller  parts,
  18.         compiling each part separately, and linking all of the parts
  19.         together into a final completed package.
  20.  
  21.              Since  human  beings  make mistakes,  and  because  the
  22.         entire  program must be recompiled each time any mistake  is
  23.         discovered,  pure  Pascal  is  unsuitable  for  very   large
  24.         programs.   Seeing this problem, many compiler writers  have
  25.         defined some method by which a large program could be broken
  26.         down into smaller parts and separately compiled.
  27.  
  28.              This chapter will define and illustrate the way Borland
  29.         has chosen to do so.
  30.  
  31.                              PART OF A PROGRAM
  32.  
  33.              Load  the  program named AREAS and display it  on  your
  34.         monitor.  This is the first example of a TURBO Pascal "unit"
  35.         and although it is similar to a program in many ways, it has
  36.         a few differences which must be pointed out.  We will  start
  37.         by  pointing  out  the major sections,  then  get  into  the
  38.         details of each section.
  39.  
  40.              You will first notice that this program begins with the
  41.         reserved  word  "unit"  instead  of  our  unusal  "program",
  42.         followed by the unit name, Areas.  In line 10, the  reserved
  43.         word "interface" is used and all of the statements following
  44.         it down to the next reserved word "implementation", are part
  45.         of the interface with any program outside of this unit.  The
  46.         next  reserved word used is "implementation" and  gives  the
  47.         definitions  and executable parts of the private portion  of
  48.         the unit.
  49.  
  50.              Finally,  in lines 48 through 50, we find what  appears
  51.         to  be  a  program block just like we have  been  using  all
  52.         through this tutorial, but actually is not.  We will see  in
  53.         a few paragraphs that this is the initialization section and
  54.         does  a  very  specific  job for  us  even  though  somewhat
  55.         different than what we have become used to.
  56.  
  57.  
  58.                                  Page 84
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                   CHAPTER 13 - Units in TURBO Pascal 4.0
  69.  
  70.  
  71.  
  72.                              THE INTERFACE PART
  73.  
  74.              Following  the unit name we have a section of  code  in
  75.         lines 10 through 15 that define the interface of this module
  76.         to the outside world.  Anything defined here is available to
  77.         the  outside  world  and can be used by  any  other  program
  78.         provided it has a "uses Areas;" statement in it.  Constants,
  79.         types, and variables could also be defined here, and if they
  80.         were,  they too would be available to any user program,  but
  81.         in this case, only the four functions are made available. It
  82.         should  be fairly obvious that the functions  calculate  the
  83.         areas of four different geometric shapes.
  84.  
  85.                           THE IMPLEMENTATION PART
  86.  
  87.              From line 16 through line 47 we have the implementation
  88.         part as delineated by the reserved word "implementation" and
  89.         the   beginning   of   the   initialization   block.     The
  90.         implementation  part  is the actual workhorse  of  the  unit
  91.         since  it contains all of the executable code for  the  four
  92.         functions defined above.
  93.  
  94.              Lines 26 through 31 contain the code needed to generate
  95.         the area of a circle, and this code is no different than the
  96.         code that would be used if this function were placed in  the
  97.         declaration  part  of  any  Pascal  program.   There  is   a
  98.         difference   in  the  function  header  since   the   formal
  99.         parameters  are not repeated here.  TURBO Pascal allows  you
  100.         to either drop the formal parameters here or include them if
  101.         you  think the code would be more readable.  If you  include
  102.         them, they must be exactly as shown in the interface part or
  103.         you will get a compile error.
  104.  
  105.                              A LOCAL PROCEDURE
  106.  
  107.              In  lines  20 through 24, we have a procedure  that  is
  108.         used within one of the four functions, namely the first.  It
  109.         is  really  a stupid procedure since it really  wastes  time
  110.         setting  up linkage for the procedure call and does  nothing
  111.         that  couldn't be done just as easy with a simple  multiply,
  112.         but  it does illustrate that you can use  another  procedure
  113.         within the unit body.  The procedure Mult_Two_Numbers cannot
  114.         be  used outside of this unit because it is not included  in
  115.         the interface part of the unit.  It is, in effect, invisible
  116.         to the outside world.
  117.  
  118.              The variable My_Pi would be more correctly  represented
  119.         as a constant but it is defined as a variable to  illustrate
  120.         use  of  the  body of the unit later.  Since  My_Pi  is  not
  121.         defined  in  the  interface part of the  unit,  it  also  is
  122.  
  123.  
  124.                                  Page 85
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                   CHAPTER 13 - Units in TURBO Pascal 4.0
  135.  
  136.  
  137.         invisible  to the outside world and in fact  protected  from
  138.         accidental  corruption by a misplaced statement  in  another
  139.         program.   The procedure and the variable for all  practical
  140.         purposes have an impenetrable barrier around them protecting
  141.         them  from  unauthorized use by the outside world,  but  the
  142.         functions  internal  to this unit have free access  to  them
  143.         just as in any other program.
  144.  
  145.                          WHAT IS THE BODY USED FOR?
  146.  
  147.              Lines 48 through 50 constitute the body of the unit and
  148.         although  they  appear  to  consist  of  another  executable
  149.         program  that can be called and used, they actually  perform
  150.         another very specific and useful purpose.  It is actually an
  151.         initialization  section  and all of the statements  in  this
  152.         part  of the unit are executed once and only once, and  they
  153.         are executed when the main program is loaded.  This is  done
  154.         automatically  for  you  by the system.   There  is  no  way
  155.         provided  for you to call the statements in the  body  after
  156.         the  program has begun execution.  This is why the  variable
  157.         My_Pi  was  defined  as a variable, so  we  could  use  this
  158.         section to initialize it to a useful value.
  159.  
  160.              The body can actually have function and procedure calls
  161.         that  are  executed when the program is loaded, as  well  as
  162.         loops or conditional statements.
  163.  
  164.              If  you  would like to execute some  statements  during
  165.         initialization and again during the execution of the program
  166.         one or more times, you can write a procedure or function  to
  167.         accomplish your desires and call it at the appropriate times
  168.         in the main program.
  169.  
  170.                 SELECTIVE NAMING OF FUNCTIONS AND PROCEDURES
  171.  
  172.              If  you will study the interface part of this unit  you
  173.         will  find  that  everything you need to use  this  unit  is
  174.         contained  within  it, provided that you know  enough  about
  175.         plane  geometry  to understand the  functions.   You  should
  176.         strive  for this understanding in all of your interfaces  so
  177.         that  the implementation doesn't even require  consultation.
  178.         Keep in mind, that if you need to, you can include  comments
  179.         to further define the functions in the interface part of the
  180.         unit.
  181.  
  182.              At  this time, you should compile this unit.  You  will
  183.         have to compile it to disk rather than only to memory so  it
  184.         will  be  available for use later in this chapter.   You  do
  185.         this by using the menus to change the Compile/Destination to
  186.         the  Disk  option.  Note that it will not generate  an  .EXE
  187.  
  188.  
  189.  
  190.                                  Page 86
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                   CHAPTER 13 - Units in TURBO Pascal 4.0
  201.  
  202.  
  203.         file  but instead a .TPU file.  This is  Borland's  filename
  204.         extension for a unit.
  205.  
  206.                                 ANOTHER UNIT
  207.  
  208.              Load  the  file named PERIMS for another example  of  a
  209.         unit.   This is similar to the last except that it does  not
  210.         contain  an internal procedure, and it is composed of  three
  211.         procedures  that  calculate  the  perimeters  of   geometric
  212.         shapes,  all  of  which are visible  to  the  outside  world
  213.         because they are included in the interface part of the unit.
  214.         Once  again,  we have a private variable named My_Pi  and  a
  215.         block  of code (actually a single statement)  to  initialize
  216.         the value of My_Pi when the unit is loaded.
  217.  
  218.              Be  sure  you  compile this unit to disk  in  the  same
  219.         manner as the last and they will be ready for use.
  220.  
  221.              Now that we have several functions and procedures  that
  222.         can be used to calculate the areas or perimiters of  several
  223.         different shapes, we need a program to illustrate their use,
  224.         so if you load and display the program named GARDEN you will
  225.         have an example of their use.
  226.  
  227.                       HOW DO WE USE OUR DEFINED UNITS?
  228.  
  229.              GARDEN  is a very simple program that uses one  of  the
  230.         functions  and  one of the procedures.  The only  thing  you
  231.         must  do  is add the names of the units prior to  using  the
  232.         external functions or procedures.  Lines 16 and 17 each  use
  233.         one of our newly defined routines.  As you can see, there is
  234.         nothing  magic about the new routines, and once you  include
  235.         the unit names in a uses statement, the new routines are  in
  236.         a sense, an extension to the Pascal language.
  237.  
  238.              Compile  and  run this program and see that  it  really
  239.         does what you expect it to do.
  240.  
  241.                         ONE MORE EXAMPLE OF UNIT USE
  242.  
  243.              Load and display the program named SHAPES4 for  another
  244.         example of using a predefined unit.  In line 3, this program
  245.         includes  our new unit named Areas so all four of  the  area
  246.         functions  are  available, and in fact, all  four  are  used
  247.         within the body of the program.  This program should not  be
  248.         difficult  for  you to understand and you will  be  left  to
  249.         study it on your own.  You should observe that this  program
  250.         is  repeated in chapter 14 in a different form for users  of
  251.         TURBO Pascal 3.0.
  252.  
  253.  
  254.  
  255.  
  256.                                  Page 87
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                   CHAPTER 13 - Units in TURBO Pascal 4.0
  267.  
  268.  
  269.                        MULTIPLE USES OF AN IDENTIFIER
  270.  
  271.              Suppose  we wanted to move the variable named My_Pi  to
  272.         the  interface  section  in both of  the  units  we  defined
  273.         earlier.  Then in the program named GARDEN when we  included
  274.         both  of  the units in the uses  statement,  both  variables
  275.         named  My_Pi would be available for use so we would  have  a
  276.         bit of a problem defining which one we really meant to  use.
  277.         TURBO Pascal has a way to tell the system which one you wish
  278.         to  use by using a qualifier in much the same way  that  you
  279.         use  a  field of a record.  The  variable  name  Areas.My_Pi
  280.         would refer to that variable from the unit named Areas,  and
  281.         the  name Perims.My_Pi would refer to the variable from  the
  282.         unit named Perims.
  283.  
  284.              You  could even define a new variable of the same  name
  285.         in  your main program and refer to it by the qualified  name
  286.         Garden.My_Pi if you chose to.  This is not recommended as it
  287.         would get very confusing to you.  The compiler would be very
  288.         happy  to compile and run such a program, because  it  would
  289.         not get confused.
  290.  
  291.                                WHY USE UNITS?
  292.  
  293.              There are basically three reasons to use units in  your
  294.         programming.   First, some programs are so large  that  they
  295.         should be broken up into smaller chunks for ease of handling
  296.         and reasonable compilation size.  In fact some are so  large
  297.         that  they  cannot be compiled all at one time  since  TURBO
  298.         Pascal  has  an  upper limit of 64K of  code  which  can  be
  299.         compiled at once.  Most other compilers have a similar limit
  300.         also.
  301.  
  302.              Secondly,  once  you  complete the code  to  perform  a
  303.         certain  job, you may wish to use the same code  in  another
  304.         program to do the same job.  If you put the code in a  unit,
  305.         it is ready to simply call and use again.  This is  becoming
  306.         a  rather  important topic in software  engineering  usually
  307.         referred to as "Reusable Software".
  308.  
  309.                          THIS IS INFORMATION HIDING
  310.  
  311.              Finally, it is sometimes important to hide a portion of
  312.         code  from the rest of the program to assure that it  cannot
  313.         be  unduly  modified  by  an error  somewhere  else  in  the
  314.         program.  This too is becoming an important area of software
  315.         engineering  and  is  usually  referred  to  as  information
  316.         hiding.
  317.  
  318.  
  319.  
  320.  
  321.  
  322.                                  Page 88
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                   CHAPTER 13 - Units in TURBO Pascal 4.0
  333.  
  334.  
  335.         PROGRAMMING EXERCISE
  336.  
  337.         1.   Move  My_Pi to the interface in both units  and  change
  338.              one  of the values slightly to see if you can  read  in
  339.              the  right  one  at the  right  time.   Define  another
  340.              variable of the same name in your main program and  see
  341.              if you can differentiate between all three values.
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                                  Page 89
  389.